package TakeAwaySystem.adminoperation.business;

import TakeAwaySystem.Admin;
import TakeAwaySystem.adminoperation.AdminIOperation;
import TakeAwaySystem.User;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/26 22:33:34
 */
public class UnLockBusinessOperation implements AdminIOperation {
    @Override
    public void work(Admin o, File file) {
        List<User> userList = fileToList(file);
        System.out.println("选择你要解锁的商户");
        User user = printBusiness(userList);
        String password = user.getPassword();
        if(password.contains("*")){
            password = password.replace("*","");
            user.setPassword(password);
            System.out.println("这个商户已经被限制");
        }else{
            System.out.println("这个商户没有被限制权限");
        }
        ListToFile(userList,file);
    }
    private User printBusiness(List<User> userList){
        Scanner scanner = new Scanner(System.in);
        HashMap<Integer,User> hashMap = new HashMap<>();
        for(User user : userList){
            hashMap.put(user.getId(),user);
            System.out.println(user.getId() +"  " + user.getName());
        }
        return hashMap.get(scanner.nextInt());
    }

    @Override
    public List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
        //File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null){
                    String[] arr = str.split(" ");
                    Admin users = new Admin();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }
    @Override
    public void ListToFile(List<User> list,File file) {
        // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Admin user = (Admin) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName() + " ");
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }
}
